package com.gjy.quartz.simple;

import org.apache.commons.lang3.tuple.Pair;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Trigger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-08-11 12:21:54
 */
public class PrintTimeJob implements Job {
    private static final Logger log = LoggerFactory.getLogger(PrintTimeJob.class);

    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");

    private final List<Pair<Date, Date>> list = new ArrayList<>();

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("job: {}", context);
        log.info("time: {}", LocalDateTime.now().format(FORMATTER));
        Trigger trigger = context.getTrigger();
        Date previousFireTime = trigger.getPreviousFireTime();
        Date fireTimeAfter = trigger.getFireTimeAfter(previousFireTime);
        list.add(Pair.of(previousFireTime, fireTimeAfter));
        log.info("list: {}", list);

        Date time0 = trigger.getStartTime();
        Date time1 = trigger.getFireTimeAfter(time0);
        Date time2 = trigger.getFireTimeAfter(time1);
        Date time3 = trigger.getFireTimeAfter(time2);
        log.info("dd: {},{},{},{}", time0, time1, time2, time3);
        // Mon Aug 11 14:57:59 CST 2025,
        // Mon Aug 11 14:58:00 CST 2025,
        // Mon Aug 11 14:58:05 CST 2025,
        // Mon Aug 11 14:58:10 CST 2025

        // 2025-08-11 14:57:50,
        // 2025-08-11 14:57:55
        //上上次
        Instant lastLastInstant = Instant.ofEpochMilli(time1.getTime() - 2 * (time3.getTime() - time2.getTime()));
        LocalDateTime lastLast = LocalDateTime.ofInstant(lastLastInstant, ZoneId.systemDefault());
        String lastLastTime = lastLast.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //上次
        Instant lastInstant = Instant.ofEpochMilli(time1.getTime() - (time3.getTime() - time2.getTime()));
        LocalDateTime last = LocalDateTime.ofInstant(lastInstant, ZoneId.systemDefault());
        String lastTime = last.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        log.info("d2: {},{}", lastLastTime, lastTime);
    }

}
